//
//  WeiXinRootViewController.m
//  WeixinActivity
//
//  Created by Apple on 15/12/10.
//  Copyright © 2015年 Johnny iDay. All rights reserved.
//

#import "WeiXinRootViewController.h"
#import "ShareModel.h"
#import "WXApi.h"
#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface WeiXinRootViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIWebView *webView;
    UITableView *tableView;
    
    ShareModel *model;
    
    
    NSArray *dataArr;
}
@end

@implementation WeiXinRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    [self loadData];
    [self setData];
    // Do any additional setup after loading the view.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHT *0.4*0.6 / 3;
}
- (void)configUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem = item;
    
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 0.8, HEIGHT * 0.6 * 0.6)];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.6*0.6, WIDTH * 0.8, HEIGHT *0.4*0.6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableFooterView = [UIView new];
    [self.view addSubview:tableView];
}
- (void)loadData
{
    dataArr = @[@{@"image":[UIImage imageNamed:@"3"],@"name":@"分享到朋友圈"},@{@"image":[UIImage imageNamed:@"3"],@"name":@"分享给好友"},@{@"image":[UIImage imageNamed:@"3"],@"name":@"收藏"}];
}

- (void)setData
{
    self.title = @"选择";
    model = [ShareModel standerInstance];
    NSString *image1=[NSString stringWithFormat:@"<img src='%@'  height='100' width='150' />",model.image];
    NSString *linkStr=[NSString stringWithFormat:@"<a href='%@'>%@</a>",
                       model.url,@"点这里查看详情"];
    //初始化和html字符串
    NSString *htmlURlStr=[NSString stringWithFormat:@"<body style='background-color:#ffffff'><p>%@</p><p>%@</p><p>%@</p></body>",model.title,image1,linkStr];
    [webView loadHTMLString:htmlURlStr baseURL:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableVie cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    if(!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
    cell.imageView.image = dataArr[indexPath.row][@"image"];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.text = dataArr[indexPath.row][@"name"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = 0;
    return cell;
}
- (void)setThumbImage:(SendMessageToWXReq *)req
{
    if (model.image) {
        CGFloat width = 100.0f;
        CGFloat height = model.image.size.height * 100.0f / model.image.size.width;
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        [model.image drawInRect:CGRectMake(0, 0, width, height)];
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [req.message setThumbImage:scaledImage];
    }
}
- (void)sendReq:(enum WXScene)scene
{
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.scene = scene;
    req.message = WXMediaMessage.message;
    req.message.title = model.title;
    [self setThumbImage:req];
    if (model.url) {
        WXWebpageObject *webObject = WXWebpageObject.object;
        webObject.webpageUrl = [model.url absoluteString];
        req.message.mediaObject = webObject;
    } else if (model.image) {
        WXImageObject *imageObject = WXImageObject.object;
        imageObject.imageData = UIImageJPEGRepresentation(model.image, 1);
        req.message.mediaObject = imageObject;
    }
    [WXApi sendReq:req];
}
- (void)leftAction
{
    self.goBack();
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    enum WXScene scene;
    if(indexPath.row == 2)
        scene = WXSceneSession;
    else if(indexPath.row == 1)
        scene = WXSceneTimeline;
    else
        scene = WXSceneFavorite;
    [self sendReq:scene];
}
@end
